﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsBidsTableAdapters;

public partial class BuyerJobDetail : System.Web.UI.Page
{

    #region Constants

    private const string PROP_DATASRC_JOBHISTORY = "__JobHistory::DSJH";
    private const string PROP_DATASRC_PUBLICMESSAGE_CONTRACTOR = "__PublicMessageofContractor::DSPMC";
    private const string PROP_DATASRC_PUBLICMESSAGE_BUYER = "__PublicMessageofBuyer::DSPMB";
    private const string PROP_DATASRC_JOBINVITATION = "__JobInvitation::DSJI";
    private const string PROP_DATASRC_JOBIMAGES = "__JobImages::DSJIMAGE";
    private const string PROP_DATASRC_JOBVIDEO = "__JobVideo::DSJV";
    private const string PROP_DATASRC_JOBDOCUMENT = "__JobDocument::DSJD";
    private const string PROP_DATASRC_BIDCATEGORY = "__BidCategory::DSBC";
    private const string PROP_DATASRC_MEETINGSINFO = "__MeetingsInfo::DSMI";
    private const string PROP_DATASRC_MEETINGSACCEPTED = "__MeetingsAccepted::DSMA";
    private const string PROP_DATASRC_MEETINGSREJECTED = "__MeetingsRejected::DSMR";

    #endregion

    #region Public Properties
    
    public Job jobObj;
    public RegisteredUser BuyerObj;
    public JobCategory JobCategoryObj;
    public JobSubCategory JobSubCategoryObj;
    public BidCategory BidCatgegoryObj;

    public JobHistoryCollection DataSourceJobHistory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBHISTORY] is JobHistoryCollection ? (JobHistoryCollection)SessionMgr.Session[PROP_DATASRC_JOBHISTORY] : (JobHistoryCollection)(SessionMgr.Session[PROP_DATASRC_JOBHISTORY] = new JobHistoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBHISTORY] = value; }
    }

    public PublicMessageCollection DataSourcePublicMessageofContractor
    {
        get { return (SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_CONTRACTOR] is PublicMessageCollection ? (PublicMessageCollection)SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_CONTRACTOR] : (PublicMessageCollection)(SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_CONTRACTOR] = new PublicMessageCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_CONTRACTOR] = value; }
    }

    public PublicMessageCollection DataSourcePublicMessageofBuyer
    {
        get { return (SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_BUYER] is PublicMessageCollection ? (PublicMessageCollection)SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_BUYER] : (PublicMessageCollection)(SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_BUYER] = new PublicMessageCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_PUBLICMESSAGE_BUYER] = value; }
    }

    public JobInvitationsCollection DataSourceJobInvitations
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBINVITATION] is JobInvitationsCollection ? (JobInvitationsCollection)SessionMgr.Session[PROP_DATASRC_JOBINVITATION] : (JobInvitationsCollection)(SessionMgr.Session[PROP_DATASRC_JOBINVITATION] = new JobInvitationsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBINVITATION] = value; }
    }

    public JobAttachmentsCollection DataSourceJobImages
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBIMAGES] is JobAttachmentsCollection ? (JobAttachmentsCollection)SessionMgr.Session[PROP_DATASRC_JOBIMAGES] : (JobAttachmentsCollection)(SessionMgr.Session[PROP_DATASRC_JOBIMAGES] = new JobAttachmentsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBIMAGES] = value; }
    }

    public JobAttachmentsCollection DataSourceJobVideos
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBVIDEO] is JobAttachmentsCollection ? (JobAttachmentsCollection)SessionMgr.Session[PROP_DATASRC_JOBVIDEO] : (JobAttachmentsCollection)(SessionMgr.Session[PROP_DATASRC_JOBVIDEO] = new JobAttachmentsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBVIDEO] = value; }
    }

    public JobAttachmentsCollection DataSourceJobDocuments
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBDOCUMENT] is JobAttachmentsCollection ? (JobAttachmentsCollection)SessionMgr.Session[PROP_DATASRC_JOBDOCUMENT] : (JobAttachmentsCollection)(SessionMgr.Session[PROP_DATASRC_JOBDOCUMENT] = new JobAttachmentsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBDOCUMENT] = value; }
    }


    public BidCategoryCollection DataSourceBidCategories
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BIDCATEGORY] is BidCategoryCollection ? (BidCategoryCollection)SessionMgr.Session[PROP_DATASRC_BIDCATEGORY] : (BidCategoryCollection)(SessionMgr.Session[PROP_DATASRC_BIDCATEGORY] = new BidCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BIDCATEGORY] = value; }
    }
    public MeetingsInfoCollection DataSourceMeetingsInfo
    {
        get { return (SessionMgr.Session[PROP_DATASRC_MEETINGSINFO] is MeetingsInfoCollection ? (MeetingsInfoCollection)SessionMgr.Session[PROP_DATASRC_MEETINGSINFO] : (MeetingsInfoCollection)(SessionMgr.Session[PROP_DATASRC_MEETINGSINFO] = new MeetingsInfoCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_MEETINGSINFO] = value; }
    }

    public MeetingAcceptanceOrDeclineCollection DataSourceMeetingAccepted
    {
        get { return (SessionMgr.Session[PROP_DATASRC_MEETINGSACCEPTED] is MeetingAcceptanceOrDeclineCollection ? (MeetingAcceptanceOrDeclineCollection)SessionMgr.Session[PROP_DATASRC_MEETINGSACCEPTED] : (MeetingAcceptanceOrDeclineCollection)(SessionMgr.Session[PROP_DATASRC_MEETINGSACCEPTED] = new MeetingAcceptanceOrDeclineCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_MEETINGSACCEPTED] = value; }
    }

    public MeetingAcceptanceOrDeclineCollection DataSourceMeetingRejected
    {
        get { return (SessionMgr.Session[PROP_DATASRC_MEETINGSREJECTED] is MeetingAcceptanceOrDeclineCollection ? (MeetingAcceptanceOrDeclineCollection)SessionMgr.Session[PROP_DATASRC_MEETINGSREJECTED] : (MeetingAcceptanceOrDeclineCollection)(SessionMgr.Session[PROP_DATASRC_MEETINGSREJECTED] = new MeetingAcceptanceOrDeclineCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_MEETINGSREJECTED] = value; }
    }


    public dsBids.GetCompanyInfoByJobIDDataTable BidTable = new dsBids.GetCompanyInfoByJobIDDataTable();
    public dsBids.GetCompanyInfoByJobIDDataTable BidTableDeclined = new dsBids.GetCompanyInfoByJobIDDataTable();


    public BidCollection DataSourceContractorsBids;
    public Contracts contractObj;
    public int TotalJobCount = 0;
    public int TotalRating = 0;
    public int reviewcount = 0;

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {

            if (Session["username"] == null)
            {
                Response.Redirect("../sessionexpire.aspx");
            }
            else
            {
                Session["jobid"] = Request.QueryString["jid"].ToString();
                jobObj = Job.GetJobByID(Request.QueryString["jid"].ToString());
                //lbljobstatus.Text = jobObj.JobStatus;
                //JobCategoryObj = JobCategory.GetJobCategoryByID(jobObj.JobCategoryID);
                //JobSubCategoryObj = JobSubCategory.GetJobSubCategoryByID(jobObj.JobSubCategoryID);
                BuyerObj = RegisteredUser.GetRegisteredUserByID(jobObj.UserID_RK_tblRegisteredUsers);
                BidCatgegoryObj = BidCategory.GetBidCategoryByID(jobObj.BidCategoryID);
                DataSourceJobHistory = JobHistory.GetCompleteJobHistory(jobObj.JobID);

                //Datasource of Public Messages
                DataSourcePublicMessageofContractor = PublicMessage.GetCompletePublicMessage(jobObj.JobID, "Contractor");
                DataSourcePublicMessageofBuyer = PublicMessage.GetCompletePublicMessage(jobObj.JobID, "Buyer");
                if (DataSourcePublicMessageofContractor.Count > 0)
                    lblprofilename.Text = CompanyInformation.GetCompanyInformationByID(DataSourcePublicMessageofContractor[0].UserID).Companyname;

                // Datasource of Bids.
                GetCompanyInfoByJobIDTableAdapter adapter = new GetCompanyInfoByJobIDTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();
                BidTable = adapter.GetCompanyInfo(jobObj.JobID, null);
                BidTableDeclined = adapter.GetCompanyInfo(jobObj.JobID, "Declined");

                //Datasource for Job Invitations
                DataSourceJobInvitations = JobInvitations.GetCompleteJobInvitations(Session["jobid"].ToString());

                //Datasource for Job Attachments
                DataSourceJobImages = JobAttachments.GetJobAttachmentsByFileType(Session["jobid"].ToString(), "Image");
                DataSourceJobVideos = JobAttachments.GetJobAttachmentsByFileType(Session["jobid"].ToString(), "Video");
                DataSourceJobDocuments = JobAttachments.GetJobAttachmentsByFileType(Session["jobid"].ToString(), "Document");

                //Datasource for Bid Cagtegory.
                DataSourceBidCategories = BidCategory.GetAllBidCategories();
                LoadBidCategoryRadioButtonList();

                // Datasource for Meeting

                DataSourceMeetingsInfo = MeetingsInfo.GetMeetingsInfoByJobID(Session["jobid"].ToString());

                //Making Biding category radio button selected

                if (!Page.IsPostBack)
                {
                    for (int i = 0; i < DataSourceBidCategories.Count; i++)
                    {
                        if (rblbidcategory.Items[i].Value == BidCatgegoryObj.BidCategoryID.ToString())
                            rblbidcategory.Items[i].Selected = true;
                    }
                }

                //Calculating average price of the job
                decimal totalamount = 0, average = 0;

                for (int i = 0; i < BidTable.Count; i++)
                {
                    totalamount = totalamount + BidTable[i].BidAmount;

                }
                try
                {
                    average = totalamount / BidTable.Count;
                }
                catch (Exception ex)
                { }
                lblaverage.Text = "$ " + average.ToString() + ".";
            }

    }

    #endregion

    #region PublicMessage Save Click

    protected void bttnpublicmessagesave_Click(object sender, EventArgs e)
    {
        PublicMessage messageObj = PublicMessage.CreateNew();
        messageObj.JobID = Request.QueryString["jid"].ToString();
        messageObj.UserID = RegisteredUser.GetRegisteredUserByID(Int32.Parse(Session["currentuserid"].ToString())).RegisteredUserID;
        messageObj.MessageText = txtmessage.Text;
        messageObj.UserType = RegisteredUser.GetRegisteredUserByID(Int32.Parse(Session["currentuserid"].ToString())).RegisteredUserType; ;
        PublicMessage.Update(messageObj);
        txtmessage.Text = "";
        lblfeedback.Text = "Message Posted Successfully...";
        lblfeedback.Visible = true;
        //System.Threading.Thread.Sleep(1000);
    }
    #endregion

    #region Button Save Bid Category

    protected void bttnsavebidcategory_Click(object sender, ImageClickEventArgs e)
    {

    }

    #endregion

    #region Radio Button Bid Category Selected

    protected void rblbidcategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        jobObj = Job.GetJobByID(Request.QueryString["jid"].ToString());
        jobObj.BidCategoryID = Int32.Parse(rblbidcategory.SelectedItem.Value);
        Job.Update(jobObj);
        lblbidfeedback.Visible = true;
        Response.AddHeader("Refresh", "0");
    }

    #endregion

    #region Add Consultant Click Event

    protected void btn_consultant_Click(object sender, EventArgs e)
    {
        jobObj = Job.GetJobByID(Request.QueryString["jid"].ToString());
        if (jobObj != null)
        {
            if (Request.Form["rblistconsultant"] != null)
            {
                jobObj.ConsultantID = Request.Form["rblistconsultant"].ToString();
                Job.Update(jobObj);
            }
        }
    }

    #endregion

    #endregion

    #region Private

    #region LoadBidCategoryRadioButtonList

    protected void LoadBidCategoryRadioButtonList()
    {
        rblbidcategory.Items.Clear();
        rblbidcategory.AppendDataBoundItems = true;
        rblbidcategory.DataSource = DataSourceBidCategories;
        rblbidcategory.DataTextField = "BidCategoryName";
        rblbidcategory.DataValueField = "BidCategoryID";
        rblbidcategory.ID = "BidCategoryID";
        rblbidcategory.DataBind();
    }

    #endregion

    #endregion


    
}
